package query_handler

import (
	"github.com/marmotedu/errors"
	"time"
)

const (
	QueryByATime HandlerBuilder = iota
	QueryByMTime
	QueryByCTime
	QueryHash
	QueryForDeleteOnly
)

type HandlerBuilder int

var (
	BuildNeedWithTimeRange = errors.New("build need with time range")
)

func (b HandlerBuilder) Build() (QueryHandler, error) {
	switch b {
	case QueryByATime, QueryByMTime, QueryByCTime:
		return nil, BuildNeedWithTimeRange
	case QueryHash:
		return NewQueryHashImpl(), nil
	case QueryForDeleteOnly:
		return QueryForDeleteOnlyImpl{}, nil
	default:
		return nil, errors.New("unknown query handler builder")
	}
}

func (b HandlerBuilder) BuildWithTimeRange(after, before time.Time) (QueryHandler, error) {
	switch b {
	case QueryByATime:
		return &QueryByATimeImpl{
			AfterTime:  after,
			BeforeTime: before,
		}, nil
	case QueryByMTime:
		return &QueryByMTimeImpl{
			AfterTime:  after,
			BeforeTime: before,
		}, nil
	case QueryByCTime:
		return &QueryByCTimeImpl{
			AfterTime:  after,
			BeforeTime: before,
		}, nil
	default:
		return nil, errors.New("unknown query handler builder, or builder not need time range")
	}
}
